package com.gm.wholesale.module.custom.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.gm.commonlib.util.viewUtils.KeyboardUtils;
import com.gm.commonlib.views.popupwindow.Popupwindow;
import com.gm.wholesale.R;
import com.gm.wholesale.module.custom.bean.CustomBean;
import com.gm.wholesale.module.custom.ui.ChoiceCustomFragment;
import com.gm.wholesale.module.main.ui.MainActivity;

public class CustomView extends LinearLayout {
    public ChoiceCustomFragment mCustomFragment;
    private ImageView cancelImag;

    private EditText searchByNameEd;

    public void setCustomChoiceListener(onCustomChoiceListener listener) {
        mCustomFragment.setOnCustomChoice(listener);
    }

    public interface onCustomChoiceListener {
        void onChoiceCustom(CustomBean customBean);
    }

    public CustomView(Context context) {
        super(context);
        initView(context);
    }

    public CustomView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    private void initView(Context context) {
        View mainView = LayoutInflater.from(context).inflate(R.layout.view_costom, this);
        mainView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Popupwindow.hiddenPop();
            }
        });
        MainActivity activity = (MainActivity) context;
        mCustomFragment = (ChoiceCustomFragment) activity.getSupportFragmentManager().findFragmentById(R.id.choiceCustomFragment);
        if (mCustomFragment != null) {
            mCustomFragment.setIsGetData(true);
        }
        searchByNameEd = findViewById(R.id.searchByNameEd);
        cancelImag = findViewById(R.id.cancelImag);
        cancelImag.setOnClickListener(view -> Popupwindow.hiddenPop());
        searchByNameEd.setOnEditorActionListener((v, actionId, event) -> {

            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                String key = searchByNameEd.getText().toString();
                mCustomFragment.setKeyword(key);
                KeyboardUtils.hideKeyboard(v);
            }
            return false;


        });

    }

    public void updateView() {
        if (mCustomFragment != null) {
            mCustomFragment.refreshData();
        }
    }


}
